package top.yxqz.springmvc.annotation.util;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: 余小小
 * @date: 2023/5/18
 * @description： 这是自定义拦截器
 * 主要用于请求的预处理和后处理
 */
@Component("myhandler")
public class MyHandlerInterceptor implements HandlerInterceptor {

    /**
     * 处理预处理的操作 在controller接口执行之前
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("这是MyHandlerInterceptor——————————————preHandle");
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    /**
     * 在controller接口执行之后
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("这是MyHandlerInterceptor——————————————postHandle");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    /**
     * postHandle执行完毕过后，执行
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("这是MyHandlerInterceptor——————————————afterCompletion");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
